import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;


public class Evils {
	
	public int x,y;
	public int limX, limY;
	public int width; 
	public int height; 	
	public int COL = 3;	
	public int ROW = 4;
	public int currFrame = 0;
	public int currRow = 2;
	public int xspeed = 2;
	public int yspeed = 3;
	public int type;
	public String path;
	public Image img;
	public Sprite sprite;
	public static final int NINJA = 0;
	public static final int ANGEL = 1;
	public static final int FLYDRAGON = 2;
	public static final int FISH = 3;
	public static final int BOSS = 4;
	
	public int mstate;
	public int state_stand = 0;
	public int state_jump = 1;
	public int state_drop = 2;

	
	public int mlife ;
	
	public boolean isDraw = true;
	
	public static Vector evilVec= new Vector();
	
	public Evils(int type1)
	{
		try {
			x = initX(type1);
			y = initY(type1);
			type = type1;
			path = initPath(type1);
			img = Image.createImage(path);
			width = img.getWidth()/COL;
			height = img.getHeight()/ROW;
			
			switch (Level.currentLevel) {
			case 1:
				mlife = 3;
				break;
			case 2:
				mlife = 5;
				break;
			case 3:
				mlife = 7;
				break;
			case 4:
				mlife = 9;
				break;
			default:
				break;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

	
	public String initPath(int typePath)
	{
		String path = null;
		
		switch (typePath) {
		case NINJA:
			path = "/ninja.png";
			break;
		case ANGEL:
			path = "/angel.png";
			break;		
		case FLYDRAGON:
			path = "/flydragon.png";
			break;
		case FISH:
			path = "/fish.png";
			break;
		case BOSS:
			switch (Level.currentLevel) {
			case 1:
				path = "/boss1.png";
				break;
			case 2:
				path = "/boss2.png";
				break;
			case 3:
				path = "/boss3.png";
				break;
			case 4:
				path = "/boss4.png";

			default:
				break;
			}
			break;
		}
		
		return path;
	}
	
	public int initX(int type1)
	{
		int xx  = 0;
		int level = Level.currentLevel;

		switch (level) {
		
		case 1:
			
			switch (type1) {
			case NINJA:
				xx = loadPosTileX(0,type1);
				break;
			case ANGEL:
				xx = loadPosTileX(1,type1);
				break;	
			case BOSS:
				xx = loadPosTileX(0,type1);
				break;
			}
			break;
			
		case 2:
			
			switch (type1) {
			case NINJA:
				xx = loadPosTileX(0,type1);
				break;
			case ANGEL:
				xx = loadPosTileX(1,type1);
				break;		
			case BOSS:
				xx = loadPosTileX(0, type1);
				break;
			}
			break;
			
		case 3:
			
			switch (type1) {
			case FLYDRAGON :
				xx = loadPosTileX(0,type1);
				break;
			case NINJA:
				xx = loadPosTileX(1,type1);
				break;
			case ANGEL:
				xx = loadPosTileX(2,type1);
				break;	
			case FISH:
				xx = loadPosTileX(3,type1);
				break;
			case BOSS:
				xx = loadPosTileX(0, type1);
			}
			break;
			
		case 4:
			
			switch (type1) {
			case FLYDRAGON :
				xx = loadPosTileX(0,type1);
				break;
			case NINJA:
				xx = loadPosTileX(1,type1);
				break;
			case ANGEL:
				xx = loadPosTileX(2,type1);
				break;	
			case FISH:
				xx = loadPosTileX(3,type1);
				break;
			case BOSS:
				xx = loadPosTileX(0, type1);
			}
			break;
	
		default:
			break;
		}
		

		
		return xx;
	}
	
	public int initY(int type1)
	{
		//int yy  = 6 * TileMap.size ;
		int yy  = 0;
		int level = Level.currentLevel;

		switch (level) {
		
		case 1:
			
			switch (type1) {
			case NINJA:
				yy = loadPosTileY(0,type1);
				break;
			case ANGEL:
				yy = loadPosTileY(1,type1);
				break;		
			case BOSS:
				yy = loadPosTileY(0,type1);
				break;
			}
			break;
			
		case 2:
			
			switch (type1) {
			case NINJA:
				yy = loadPosTileY(0,type1);
				break;
			case ANGEL:
				yy = loadPosTileY(1,type1);
				break;
			case BOSS:
				yy = loadPosTileY(0,type1);
				break;
			}
			break;
			
		case 3:
			
			switch (type1) {
			case FLYDRAGON :
				yy = loadPosTileY(0,type1);
				break;
			case NINJA:
				yy = loadPosTileY(1,type1);
				break;
			case ANGEL:
				yy = loadPosTileY(2,type1);
				break;	
			case FISH:
				yy = loadPosTileY(3,type1);
				break;
			case BOSS:
				yy = loadPosTileY(0, type1);
			}
			break;
			
		case 4:
			
			switch (type1) {
			case FLYDRAGON :
				yy = loadPosTileY(0,type1);
				break;
			case NINJA:
				yy = loadPosTileY(1,type1);
				break;
			case ANGEL:
				yy = loadPosTileY(2,type1);
				break;	
			case FISH:
				yy = loadPosTileY(3,type1);
				break;
			case BOSS:
				yy = loadPosTileY(0, type1);
			}
			break;
	
		default:
			break;
		}
		
		return yy;
	}
	

	
	
	public void update()
	{
		Global.evilCurrRow = currRow;
	}
	
	
	public static int loadPosTileX(int cc, int type)
	{
		int xx ;
		int count= 0;
		for(int i = 0; i<Mscreen.heightScree; i+= TileMap.size)
		{
			for(int j = 0 ; j < Global.maplenght ; j+= TileMap.size)
			{				
				int t = TileMap.getCurrentmapFoot(i, j);
				if(type != BOSS)
				{
					if(t==19)
					{
						 Global.arrayXTile[count] = j;
						 count++;											
					}
				}	
				else
				{
					if(t==20)
					{
						Global.arrayXTile[count] = j;
						 count++;
					}
				}
			}
		}
		
		xx = Global.arrayXTile[cc];
		
		return xx;
	}
	
	public static int loadPosTileY(int cc, int type)
	{
		int yy ;
		int count= 0;
		for(int i = 0; i<Mscreen.heightScree; i+= TileMap.size)
		{
			for(int j = 0 ; j < Global.maplenght ; j+= TileMap.size)
			{				
				int t = TileMap.getCurrentmapFoot(i, j);
				if(type != BOSS)
				{
					if(t==19)
					{
						 Global.arrayYTile[count] = i + TileMap.lech ;
						 count++;											
					}
				}
				else
				{
					if(t==20)
					{
						 Global.arrayYTile[count] = i + TileMap.lech ;
						 count++;	
					}
				}
				
			}
		}
		
		yy = Global.arrayYTile[cc];
		
		return yy;
	}
	
	
	public void autoDrop()
	{
		if(!TileMap.testTileStand(TileMap.getCurrentmapFoot(y+height - TileMap.lech,x+width/2)))
		{				
			if(y + height < Mscreen.heightScree)
			{
				y += yspeed;
				if(y + height > Mscreen.heightScree)
				{					
					int kc = Math.abs(Mscreen.heightScree - (y + height));
					y -= kc;
				}
				currRow = 0;
			}
		}
	}
	
	public boolean isCollison(int x2, int y2, int w2, int h2, int type1)
	{		
		boolean coll;
		if(type== BOSS)
		{
			if((x2 + w2 >= x && x2+w2/2<=x+width) && (y2+h2/2>=y && y2+h2/2<=y+height) )
			{				
				coll = true;
			}
			else
			{
				coll = false;
			}
		}
		else
		{	
			if(type1 == Boom.DAN)
			{
				if((x2 + w2 >= x && x2+w2/2 <= x + width) && (y2+h2/2>=y && y2+h2/2<=y+height))
				{
					coll = true;
				}
				else
				{
					coll = false;
				}
			}
			else
			{
				if((x2 + w2/2 >= x && x2+w2/2 <= x + width) && (y2+h2>=y && y2+h2<=y+height/2))
				{
					coll = true;
				}
				else
				{
					coll = false;
				}
			}
			
		}		
	
		
		return coll;
	}
	
	



	
	public void draw(Graphics g)
	{
		if(isDraw)
		{
			update();
			g.drawRegion(img, currFrame * width, currRow * height, width, height, 0, x, y, Graphics.TOP|Graphics.LEFT);
		}
	}
}
